import requests
import json
import random
token = requests.session()

url = " http://localhost:5000/product"
#商品
id_list = [1,5,7,11,12]
id_good = random.choice(id_list)
#价格
def price():
    if random.random() < 0.3:
        lower = 3000
        upper = 4000
    elif random.random() < 0.7:
        lower = 4000
        upper = 6000
    else:
        lower = 6000
        upper = 8000
    return random.randint(lower, upper)
price_value = price()
#数量
num = random.randint(1, 10)
body = {
    "id": id_good,
    "price": price_value,
    "amount": num,
}
res = token.post(url = url,data = body)
http = res.status_code#响应状态码
res_body = res.text#响应信息
json_data = json.loads(res_body)#数据转换为字典

print(f'购买{json_data["id"]}号商品,共{json_data["amount"]}件,单价{json_data['price']}元,共花费{json_data['money']}元')

